package com.yic.module.device.controller.admin.deviceCategory;

import com.yic.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.deviceCategory.vo.*;
import com.yic.module.device.dal.dataobject.deviceCategory.DeviceCategoryDO;
import com.yic.module.device.convert.deviceCategory.DeviceCategoryConvert;
import com.yic.module.device.service.deviceCategory.DeviceCategoryService;

@Tag(name = "管理后台 - 设备分类")
@RestController
@RequestMapping("/device/category")
@Validated
public class DeviceCategoryController {

    @Resource
    private DeviceCategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备分类")
    @PreAuthorize("@ss.hasPermission('device:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody DeviceCategoryCreateReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备分类")
    @PreAuthorize("@ss.hasPermission('device:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody DeviceCategoryUpdateReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:category:query')")
    public CommonResult<DeviceCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        DeviceCategoryDO category = categoryService.getCategory(id);
        return success(DeviceCategoryConvert.INSTANCE.convert(category));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备分类列表")
    @PreAuthorize("@ss.hasPermission('device:category:query')")
    public CommonResult<List<DeviceCategoryRespVO>> getCategoryList(DeviceCategoryReqVO reqVO) {
        List<DeviceCategoryDO> list = categoryService.getDeviceCategoryList(reqVO);
        return success(DeviceCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取设备分类精简信息列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<DeviceCategorySimpleRespVO>> getSimpleItemCategoryList() {
        // 获得分类列表，只要开启状态的
        DeviceCategoryReqVO reqVO = new DeviceCategoryReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<DeviceCategoryDO> list = categoryService.getDeviceCategoryList(reqVO);
        // 返回给前端
        return success(DeviceCategoryConvert.INSTANCE.convertList03(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分类分页")
    @PreAuthorize("@ss.hasPermission('device:category:query')")
    public CommonResult<PageResult<DeviceCategoryRespVO>> getCategoryPage(@Valid DeviceCategoryPageReqVO pageVO) {
        PageResult<DeviceCategoryDO> pageResult = categoryService.getCategoryPage(pageVO);
        return success(DeviceCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备分类 Excel")
    @PreAuthorize("@ss.hasPermission('device:category:export')")
    @OperateLog(type = EXPORT)
    public void exportCategoryExcel(@Valid DeviceCategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DeviceCategoryDO> list = categoryService.getCategoryList(exportReqVO);
        // 导出 Excel
        List<DeviceCategoryExcelVO> datas = DeviceCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备分类.xls", "数据", DeviceCategoryExcelVO.class, datas);
    }

}
